package com.example.cloudorder.web;

import com.example.cloudorder.vo.Info;
import com.example.cloudorder.vo.Type;
import com.example.cloudorder.vo.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class OrderAction {

    @RequestMapping("order")
    public String order(HttpServletRequest request){
        return "order : "+ request.getServerPort();
    }

    @Resource
    RestTemplate restTemplate;
    @RequestMapping("index")
    public String index(){
        String url = "http://cloud-index/index";
        return restTemplate.getForObject(url, String.class);
    }

    @RequestMapping("hello")
    public String hello(String name){
        return "hello " + name;
    }

    @RequestMapping("hello/{name}")
    public String hello1(@PathVariable String name){
        return "你好 " + name;
    }

    @RequestMapping("add")
    public int add(int a, int b){
        return a+b;
    }

    @RequestMapping("user")
    public User user(@RequestBody User user){
        user.setAge(user.getAge() + 10);
        return user;
    }

    @RequestMapping("user1")
    public User user1(@RequestBody User user, int age, char gender){
        user.setAge(age);
        user.setGender(gender);
        return user;
    }

    @RequestMapping("test")
    public Map<String,Object> test(@RequestBody Info info){
        Map<String,Object> ret = new HashMap<>();
        ret.put("user", info.getUser());
        ret.put("type", info.getType());
        return ret;
    }

}
